<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-select v-model="queryParams.brand" placeholder="请选择品牌" clearable>
          <el-option
            v-for="vehicle in delVehicles"
            :key="vehicle.dictCode"
            :label="vehicle.dictLabel"
            :value="vehicle.dictCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="status in delVehiclesStatus"
            :key="status.dictSort"
            :label="status.dictLabel"
            :value="status.dictSort"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择车型" clearable>
          <el-option
            v-for="vehicle in delVehiclesType"
            :key="vehicle.dictCode"
            :label="vehicle.dictLabel"
            :value="vehicle.dictCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="购车年份" prop="purchaseYear">
        <el-date-picker clearable
          v-model="queryParams.purchaseYear"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择购车年份">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="座位数" prop="seatCount">
        <el-input
          v-model="queryParams.seatCount"
          placeholder="请输入座位数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vehicles:vehicles:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vehicles:vehicles:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehiclesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="购车年份" align="center" prop="purchaseYear" width="180" />
      <el-table-column label="单位名称" align="center" prop="organizationName" />
      <el-table-column label="车辆状态" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <span>{{ getStatusText(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆类型" align="center" prop="type" />
      <el-table-column label="座位数" align="center" prop="seatCount" />
      <el-table-column label="图片" align="center" width="120">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.image"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vehicles:vehicles:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vehicles:vehicles:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增车辆对话框 -->
    <add-vehicle-dialog
      :open.sync="addDialogOpen"
      :delVehicles="delVehicles"
      :delVehiclesType="delVehiclesType"
      @submit="handleAddSubmit"
    />

    <!-- 修改车辆对话框 -->
    <edit-vehicle-dialog
      :open.sync="editDialogOpen"
      :delVehicles="delVehicles"
      :delVehiclesType="delVehiclesType"
      :vehicleData="selectedVehicle"
      @submit="handleEditSubmit"
    />
  </div>
</template>

<script>
import { listVehicles, getVehicles, delVehicles, addVehicles, updateVehicles } from "@/api/vehicles/vehicles";
import { getDicts } from "@/api/system/dict/data";
import AddVehicleDialog from "./AddVehicleDialog.vue";
import EditVehicleDialog from "./EditVehicleDialog.vue";

export default {
  name: "Vehicles",
  components: {
    AddVehicleDialog,
    EditVehicleDialog
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆表格数据
      vehiclesList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        licensePlate: null,
        brand: null,
        purchaseYear: null,
        status: null,
        type: null,
        seatCount: null,
      },
      // 车辆品牌下拉框数据
      delVehicles: [],
      // 车辆类型下拉框数据
      delVehiclesType: [],
      // 车辆状态下拉框数据
      delVehiclesStatus: {},
      // 新增对话框显示状态
      addDialogOpen: false,
      // 修改对话框显示状态
      editDialogOpen: false,
      // 选中的车辆数据
      selectedVehicle: {}
    };
  },
  created() {
    this.getList();
    this.getDicts();
  },
  methods: {
    // 根据状态值获取对应的文字描述
    getStatusText(status) {
      const statusItem = this.delVehiclesStatus.find(item => item.dictSort === status);
      return statusItem ? statusItem.dictLabel : "未知状态";
    },
    /** 根据词典查找车辆品牌 */
    getDicts() {
      getDicts('vehicle_brand').then(response => {
        this.delVehicles = response.data;
      });
      getDicts('vehicle_type').then(response => {
        this.delVehiclesType = response.data;
      });
      getDicts('vehicle_status').then(response => {
        this.delVehiclesStatus = response.data;
      });
    },
    /** 查询车辆列表 */
    getList() {
      this.loading = true;
      listVehicles(this.queryParams).then(response => {
        this.vehiclesList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        console.error("获取车辆列表失败:", error);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.addDialogOpen = false;
      this.editDialogOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        licensePlate: null,
        brand: null,
        purchaseYear: null,
        type: null,
        image: null,
        seatCount: null,
        brandId: null,
        typeId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addDialogOpen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getVehicles(id).then(response => {
        this.selectedVehicle = response.data;
        this.editDialogOpen = true;
      });
    },
    /** 新增提交按钮 */
    handleAddSubmit(form) {
      console.log("form:", form);
      addVehicles(form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.addDialogOpen = false;
        this.getList();
      });
    },
    /** 修改提交按钮 */
    handleEditSubmit(form) {
      updateVehicles(form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.editDialogOpen = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车辆编号为"' + ids + '"的数据项？').then(() => {
        return delVehicles(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
